package view;

import Model.OrcUtils.JDBCUtils;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CreateUser extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CreateUser frame = new CreateUser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CreateUser() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");

        textField = new JTextField();
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("\u5BC6  \u7801");

        JButton btnNewButton = new JButton("\u6CE8\u518C");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createUseractionPerformed(e);
            }
        });

        JLabel lblNewLabel_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801");

        passwordField = new JPasswordField();

        passwordField_1 = new JPasswordField();
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(58)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                .addComponent(lblNewLabel_1)
                                                .addComponent(lblNewLabel))
                                        .addComponent(lblNewLabel_2))
                                .addGap(34)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                                        .addComponent(btnNewButton, Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(passwordField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                                        .addComponent(textField, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                                        .addComponent(passwordField_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                                                .addPreferredGap(ComponentPlacement.RELATED)))
                                .addContainerGap(163, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(45)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(30)
                                .addComponent(btnNewButton)
                                .addContainerGap(54, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }

    private void createUseractionPerformed(ActionEvent e) {
        String name = textField.getText();
        String pwd = passwordField.getText();
        if (name.isEmpty()){
            JOptionPane.showMessageDialog(null,"用户名不能为空");
            return;
        }
        if (pwd.isEmpty()){
            JOptionPane.showMessageDialog(null,"密码不能为空");
            return;
        }
        if (!pwd.equals(passwordField_1.getText())){
            JOptionPane.showMessageDialog(null,"密码不一致");
            return;
        }
        String sql = "insert into USERDATA values(?,?,?)";
        int count = JDBCUtils.DataChange(sql,name,pwd,"普通管理员");
        if (count>=0){
            JOptionPane.showMessageDialog(null,"注册成功");
        } else {
            JOptionPane.showMessageDialog(null,"注册失败");
        }
    }
}
